// ShowPicture1View.cpp : implementation of the CShowPicture1View class
//

#include "stdafx.h"
#include "ShowPicture1.h"

#include "ShowPicture1Doc.h"
#include "ShowPicture1View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShowPicture1View

IMPLEMENT_DYNCREATE(CShowPicture1View, CScrollView)

BEGIN_MESSAGE_MAP(CShowPicture1View, CScrollView)
	//{{AFX_MSG_MAP(CShowPicture1View)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShowPicture1View construction/destruction

CShowPicture1View::CShowPicture1View()
{
	// TODO: add construction code here

}

CShowPicture1View::~CShowPicture1View()
{
}

BOOL CShowPicture1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShowPicture1View drawing

#if 0
void CShowPicture1View::OnDraw(CDC* pDC)
{
	CShowPicture1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}
#endif

void CShowPicture1View::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CShowPicture1View diagnostics

#ifdef _DEBUG
void CShowPicture1View::AssertValid() const
{
	CScrollView::AssertValid();
}

void CShowPicture1View::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CShowPicture1Doc* CShowPicture1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShowPicture1Doc)));
	return (CShowPicture1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShowPicture1View message handlers

void CShowPicture1View::OnFileOpen() 
{
	// TODO: Add your command handler code here
	TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
	CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter);

	if( dlg.DoModal() == IDOK )
	{
		strFilename = dlg.GetFileName();
		Invalidate();
	}
}

void CShowPicture1View::OnDraw(CDC* pDC)
{
	CShowPicture1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	if( strFilename != "" )
	{
		BITMAP bmpProperties;

		// Create a bitmap handle using the name of the file
		HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
			                               strFilename,
					               IMAGE_BITMAP,
					               0,
						       0,
						       LR_LOADFROMFILE);

		CBitmap bmpPicture;
		CDC mdcPicture;

	// Convert the Win32 bitmap handle into an MFC bitmap object
		CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);
		bmpPicture.Attach(bmpHandle);

	// Call the Win32 GetObject() function to get the properties
		// of the bitmap and store them in a BITMAP structure
		::GetObject(bmpPicture,
			    sizeof(bmpProperties),
			    &bmpProperties);

		// Create a compatible device context
		mdcPicture.CreateCompatibleDC(pDC);
		// Select the bitmap into the device context
		CBitmap * bmpPrevious = 
				mdcPicture.SelectObject(bmpFromHandle);

		// Using the dimensions store in the BITMAP object,
		// display the picture
		pDC->BitBlt(0, 0,
			    bmpProperties.bmWidth, bmpProperties.bmHeight,
			    &mdcPicture, 0, 0, SRCCOPY);

		// Get the dimensions of the new picture
		SIZE sizeTotal;
		sizeTotal.cx = bmpProperties.bmWidth;
		sizeTotal.cy = bmpProperties.bmHeight;
		// Change the scrolling area/dimensions of the view
		SetScrollSizes(MM_TEXT, sizeTotal);

		// Restore the bitmap
		pDC->SelectObject(bmpPrevious);
		// Delete the Win32 bitmap handle
		DeleteObject(bmpHandle);
	}
}
